package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @Description UserController
 * @Author xxxx
 * @Date 2024-10-24
 */

@Slf4j
@Api(tags = "用户信息")
@RestController
@RequestMapping("user")
//构造器注入：好处：创建当前对象的时候直接给属性注入数据，spring官方推荐，但是不可以用在junit单元测试中
//实现步骤：1.在类上使用@RequicredArgsConstructor(lombok)
//        2.属性要使用final修饰
//底层原理：编译是会自动生成构造器 public RoleController（final修饰的属性类型参数1，final修饰的属性类型参数2...）
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 用户添加
     * @param userDto
     * @return
     */
    @PutMapping
    @ApiOperation("用户添加")
    @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "UserDto")
    @ApiOperationSupport(includeParameters = {"userDto.realName","userDto.RoleVoIds","userDto.deptNo","userDto.postNo","userDto.dataState","userDto.email","userDto.mobile"})
    public ResponseResult<UserVo> addUser(@RequestBody UserDto userDto){
        userService.addUser(userDto);
        return ResponseResult.success();
    }

    /**
     * 用户分页
     * @param userDto
     * @return
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation("用户分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "userDto"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {"userDto.DataState","userDto.deptNo","userDto.email","userDto.postNo","userDto.RealName","userDto.RoleId"})
    public ResponseResult<PageResponse<UserDto>> findUserVoPage(
            @RequestBody UserDto userDto,
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize){

        log.info("用户分页，请求参数：userDto={}，pageNum={}，pageSize={}",userDto,pageNum,pageSize);

        //调用业务获取分页数据
        PageResponse<UserVo> pageResponse = userService.findUserVoPage(userDto,pageNum,pageSize);
        return ResponseResult.success(pageResponse);
    }

    /**
     * 用户修改
     * @param userDto
     * @return
     */
    @PatchMapping
    @ApiOperation("用户修改")
    @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "UserDto")
    @ApiOperationSupport(includeParameters = {"userDto.email","userDto.dataState","userDto.deptNo","userDto.deptPostUserVoSet","userDto.mobile","userDto.postNo","userDto.realName","userDto.roleVoIds"})
    public ResponseResult<Boolean> updateUser(@RequestBody UserDto userDto){
        Boolean flag = userService.updateUser(userDto);
        return ResponseResult.success(flag);
    }

    /**
     * 启用或禁用用户
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation("启用或禁用用户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path",name = "id",value = "用户id",dataType = "Long"),
            @ApiImplicitParam(paramType = "path",name = "status",value = "数据状态",dataType = "String")
    })
    @ApiOperationSupport(includeParameters = {"id,status"})
    public ResponseResult startOrBan(@PathVariable("id") Long id,@PathVariable("status") String status){
        userService.startOrBan(id,status);
        return ResponseResult.success();
    }
/*    *//**
     * 用户列表
     * @param userDto
     * @return
     *//*
    @PostMapping("/list")
    @ApiOperation("用户列表")
    @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "userDto")
    @ApiOperationSupport(includeParameters = {"userDto.realName"})
    public ResponseResult userList(@RequestBody UserDto userDto){
        return userService.userList(userDto);
    }*/
}
